package net.wanglu.www.zzz.service.se.expl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import net.wanglu.www.zzz.service._Service;
import net.wanglu.www.zzz.service.se.__SentenceElements;
import edu.stanford.nlp.trees.EnglishGrammaticalRelations;
import edu.stanford.nlp.trees.GrammaticalRelation;
import edu.stanford.nlp.trees.TypedDependency;

public class _ExplExtracter extends _Service {

	private _Service commonExplExtracter = null;

	public _Service getCommonExplExtracter() {
		return commonExplExtracter;
	}
	public void setCommonExplExtracter(_Service commonExplExtracter) {
		this.commonExplExtracter = commonExplExtracter;
	}

	@Override
	public Object service(Object... objects) throws Exception {
		// TODO Auto-generated method stub
		HashMap<?, ?> preprocessData = (HashMap<?, ?>) objects[0];
		__SentenceElements se = null;
		if ( objects.length >1) {
			se = (__SentenceElements) objects[1];
		} else {
			se = new __SentenceElements();
		}
		
		ArrayList<_ExplTuple> ExplTupleList = new ArrayList<_ExplTuple>(8);
		Collection<?> tdl = (Collection<?>) preprocessData.get("tdl");
		for( Object obj : tdl) {
			TypedDependency TD = (TypedDependency) obj;
			GrammaticalRelation reln = TD.reln();
			if ( reln.equals(EnglishGrammaticalRelations.EXPLETIVE)) {
				/*
				 * 1.调用expl解析器
				 * 2.将结果添加到ExplTupleList
				 */
				_ExplTuple et = (_ExplTuple) this.commonExplExtracter.service(preprocessData, TD);
				if ( et != null) ExplTupleList.add(et);
			}
		}
		
		se.ExplTupleList.addAll(ExplTupleList);
		return se;
	}
}
